﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.oilStoreActivity
{
    public partial class Edit : EditPagebase<IStoreActivityContract, StoreActivity>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(StoreActivity entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_Introduction.SetValue(entity.Introduction);
            if (entity.StartTime != null)
                ed_StartTime.SetValue(entity.StartTime.TimeToStringDate());
            if (entity.EndTime != null)
                ed_EndTime.SetValue(entity.EndTime.TimeToStringDate());
            ed_HeadImg.SetValue(entity.Url);
            //ed_Url.SetValue(entity.Url);
            //ed_Url.SetValue(entity.Url);
        }

        protected override OperationResult SaveData(StoreActivity entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            entity.Introduction = ed_Introduction.GetValue().ToString();
            entity.StartTime = ed_StartTime.GetValue().ToNullableDateTime();
            if (entity.StartTime == DateTime.MinValue)
                entity.StartTime = null;
            entity.EndTime = ed_EndTime.GetValue().ToNullableDateTime();
            if (entity.EndTime == DateTime.MinValue)
                entity.EndTime = null;
            //entity.Url = ed_Url.GetValue().ToString();
            entity.Url = string.IsNullOrEmpty(ed_HeadImg.GetValue()) ? "~/res/images/nopic.jpg" : ed_HeadImg.GetValue();
            return base.SaveData(entity);

        }

        protected void filePhoto0_FileSelected(object sender, EventArgs e)
        {
            UploadFile.UploadImg(this.filePhoto0, ed_HeadImg);
            filePhoto0.Reset();
        }
    }
}
